package com.singfan.core.service.impl;

import com.singfan.common.util.EncryptionUtil;
import com.singfan.common.util.StringUtil;
import com.singfan.core.dao.database.*;
import com.singfan.core.dao.database.model.*;
import com.singfan.core.dao.database.model.constant.BusinessStatusType;
import com.singfan.core.dao.database.model.constant.FilterType;
import com.singfan.core.dao.mongo.IShopLocationDao;
import com.singfan.core.service.IShopService;
import com.singfan.core.util.AccessTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Author: hansonghe
 * Date: 2015-08-24 12:08
 * Desc:
 */
@Service
public class ShopServiceImpl implements IShopService {
    @Autowired
    private IShopDao shopDao;
    @Autowired
    private IShopHairDao shopHairDao;
    @Autowired
    private IShopLocationDao shopLocationDao;
    @Autowired
    private IShopHairServiceDao shopHairServiceDao;
    @Autowired
    private IShopTokenDao shopTokenDao;
    @Autowired
    private IShopAdditionalDao shopAdditionalDao;
    @Autowired
    private IBarberDao barberDao;
    @Autowired
    private IBarberHairstyleDao barberHairstyleDao;

    @Override
    public List<ShopHair> findShopHairList(int serviceType, int listOrderType, int cityId, int businessCircleId, double longitude, double latitude, int pageIndex, int pageSize) {
        List<ShopHair> shopHairList;
        if (listOrderType == FilterType.DISTANCE) {
            List<Long> shopIdList = shopLocationDao.findShopNearbyLocation(serviceType, cityId, businessCircleId, longitude, latitude, pageIndex, pageSize);
            shopHairList = shopHairDao.selectShopHairList(shopIdList);
        }
        else {
            shopHairList = shopHairDao.selectShopHairList(serviceType, listOrderType, cityId, businessCircleId, pageIndex, pageSize);
        }
        return shopHairList;
    }

    @Override
    public ShopHair findShopHair(long shopId) {
        return shopHairDao.selectShopHair(shopId);
    }

    @Override
    public Shop findShop(long shopId){
        return shopDao.selectShop(shopId);
    }

    @Override
    public Shop findShop(String phoneNumber) {
        return shopDao.selectShop(phoneNumber);
    }

    @Override
    public List<ShopHairService> selectShopHairServiceList(long shopId){
        return shopHairServiceDao.selectShopAllHairService(shopId);
    }

    @Override
    public List<ShopHair> findShopHairList(List<Long> shopIdList) {
        return shopHairDao.selectShopHairList(shopIdList);
    }

    @Override
    public List<ShopHair> searchShop(String searchText, int pageIndex, int pageSize) {
        return shopHairDao.search(searchText, pageIndex, pageSize);
    }

    @Override
    public List<Shop> findAllShop() {
        return shopDao.selectAll();
    }

    @Override
    public List<ShopHair> findSlaveShopHairList(int masterShopId, int pageIndex, int pageSize, double longitude, double latitude) {
        List<Long> shopIdList = shopLocationDao.findShopNearbyLocation(masterShopId, longitude, latitude, pageIndex, pageSize);
        return shopHairDao.selectShopHairList(shopIdList);
    }

    @Override
    public List<ShopHairService> selectShopHairServiceList(long shopId, List<Integer> serviceIdList) {
        return shopHairServiceDao.selectShopHairServiceList(shopId, serviceIdList);
    }

    @Override
    public String createAccessToken(long shopId, int cityId, int status){
        String accessToken = AccessTokenUtil.createShopAccessToken(shopId);
        shopTokenDao.insertOrUpdateToken(shopId, accessToken, cityId, status);
        return accessToken;
    }

    @Override
    public ShopToken findShopToken(String accessToken) {
        return shopTokenDao.findShopToken(accessToken);
    }

    @Override
    public void changePwd(long shopId, String pwd) {
        String passwordSalt = UUID.randomUUID().toString().substring(0, 7);
        String password = EncryptionUtil.md5(pwd + "-" + passwordSalt);

        shopDao.updatePwd(shopId, password, passwordSalt);
    }

    @Override
    public void addHairService(ShopHairService shopHairService) {
        shopHairServiceDao.create(shopHairService);
    }

    @Override
    public ShopHairService findShopHairService(int serviceId) {
        return shopHairServiceDao.selectShopHairService(serviceId);
    }

    @Override
    public void updateShopHairService(ShopHairService shopHairService) {
        shopHairServiceDao.updateShopHairService(shopHairService);
    }

    @Override
    public void deleteShopHairService(int serviceId) {
        shopHairServiceDao.deleteShopHairService(serviceId);
    }

    @Override
    public List<Shop> findSlaveShopList(int masterShopId) {
        return shopDao.selectSlaveShopList(masterShopId);
    }

    @Override
    public void createShop(Shop shop) {
        Date now = new Date();

        shopDao.create(shop);

        List<Integer> serviceValueList = new ArrayList<>();
        serviceValueList.add(1);
        serviceValueList.add(2);
        serviceValueList.add(3);
        serviceValueList.add(4);
        serviceValueList.add(5);

        shopLocationDao.createShopLocation(shop.getMasterShopId(), shop.getId(), shop.getCityId(), shop.getBusinessCircleId(), serviceValueList, shop.getLongitude().doubleValue(), shop.getLatitude().doubleValue());

        ShopAdditional shopAdditional = new ShopAdditional();
        shopAdditional.setShopId(shop.getId());
        shopAdditional.setCompanyType(shop.getCompanyType());
        if (StringUtil.isNullOrEmpty(shop.getSlaveShopName())){
            shopAdditional.setCompanyName(shop.getMasterShopName());
        }
        else{
            shopAdditional.setCompanyName(shop.getMasterShopName() + shop.getSlaveShopName());
        }
        shopAdditional.setVerifyStatus(BusinessStatusType.VERIFY_PASSED);
        shopAdditional.setCreateTime(now);
        shopAdditional.setUpdateTime(now);
        shopAdditionalDao.create(shopAdditional);

        ShopHair shopHair = new ShopHair();
        shopHair.setShopId(shop.getId());
        shopHair.setMasterShopId(shop.getMasterShopId());
        shopHair.setMasterShopName(shop.getMasterShopName());
        shopHair.setSlaveShopName(shop.getSlaveShopName());
        shopHair.setPreferredImg(shop.getShopIconUrl());
        shopHair.setImgList(shop.getImgList());
        shopHair.setDescription(shop.getDescription());
        shopHair.setAddress(shop.getAddress());
        shopHair.setLongitude(shop.getLongitude());
        shopHair.setLatitude(shop.getLatitude());
        shopHair.setCityId(shop.getCityId());
        shopHair.setDistrictId(shop.getDistrictId());
        shopHair.setBusinessCircleId(shop.getBusinessCircleId());
        shopHair.setStatus(BusinessStatusType.VERIFY_PASSED);
        shopHair.setBarberCount(0);
        shopHair.setOrderCount(0);
        shopHair.setHonor(shop.getHonor());
        shopHair.setCommentCount(0);
        shopHair.setCommentScore(0);
        shopHair.setHairCut(true);
        shopHair.setHairPerm(true);
        shopHair.setHairDye(true);
        shopHair.setHairNurse(true);
        shopHair.setHairModeling(true);
        shopHair.setMan30(false);
        shopHair.setMan60(false);
        shopHair.setMan90(false);
        shopHair.setCreateTime(now);
        shopHairDao.create(shopHair);
    }

    @Override
    public boolean isExistShopByName(int masterShopId, String slaveShopName) {
        return shopDao.isExistShopByName(masterShopId, slaveShopName);
    }

    @Override
    public boolean isExistShopByPhone(String phone) {
        return shopDao.isExistShopByPhone(phone);
    }

    @Override
    public void updateShopInfo(Shop shop) {
        shopDao.update(shop);

        ShopHair shopHair = shopHairDao.selectShopHair(shop.getId());
        shopHair.setMasterShopId(shop.getMasterShopId());
        shopHair.setMasterShopName(shop.getMasterShopName());
        shopHair.setSlaveShopName(shop.getSlaveShopName());
        shopHair.setPreferredImg(shop.getShopIconUrl());
        shopHair.setImgList(shop.getImgList());
        shopHair.setDescription(shop.getDescription());
        shopHair.setAddress(shop.getAddress());
        shopHair.setLongitude(shop.getLongitude());
        shopHair.setLatitude(shop.getLatitude());
        shopHair.setCityId(shop.getCityId());
        shopHair.setDistrictId(shop.getDistrictId());
        shopHair.setBusinessCircleId(shop.getBusinessCircleId());
        shopHair.setHonor(shop.getHonor());
        shopHairDao.update(shopHair);

        barberDao.updateShopInfo(shop.getId(), shop.getMasterShopName(), shop.getSlaveShopName(), shop.getCityId(), shop.getDistrictId(), shop.getBusinessCircleId(), shop.getAddress(), shop.getLongitude(), shop.getLatitude());
    }

    @Override
    public void forbidShop(long shopId, boolean isForbid) {
        int status;
        List<Barber> barberList;

        if (isForbid){
            status = BusinessStatusType.FORBIDDEN;
            barberList = barberDao.selectShopBarberList(shopId, BusinessStatusType.VERIFY_PASSED);
        }
        else {
            status = BusinessStatusType.VERIFY_PASSED;
            barberList = barberDao.selectShopBarberList(shopId, BusinessStatusType.FORBIDDEN);
        }

        shopDao.updateStatus(shopId, status);
        shopHairDao.updateStatus(shopId, status);

        for (Barber barber : barberList){
            barberDao.updateBarberStatus(barber.getId(), status);
            barberHairstyleDao.updateStatusByBarberId(barber.getId(), status);
        }
    }

    @Override
    public void resetPwd(long shopId) {
        String passwordSalt = UUID.randomUUID().toString().substring(0, 7);
        String password = EncryptionUtil.md5("123456" + "-" + passwordSalt);
        shopDao.resetPwd(shopId, password, passwordSalt);
    }
}
